<template>
  <div>
    <el-form :rules="rules" ref="Manager" :model="Manager" class="loginContainer"
             v-loading="loading" element-loading-text="正在连接外星网络">
      <h3 class="loginTitle">系统管理员登陆</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="Manager.username" clearable maxlength="15"
                  placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="Manager.password" show-password clearable maxlength="15"
                  placeholder="请输入密码" @keypress.native.enter="login()"></el-input>
      </el-form-item>
      <el-button type="primary" style="width: 100%" @click="login('Manager')">登录</el-button>
    </el-form>
  </div>
</template>

<script>
    import {postRequest} from "../../utils/api";

    export default {
        name: "ManagerLogin",
        data() {
            return {
                Manager: {
                    username: '',
                    password: '',
                    code: '',
                    captchaUrl: ''
                },
                loading: false,
                rules: {
                    username: [{required: true, message: '用户名不可为空', trigger: 'blur'}],
                    password: [{required: true, message: '密码不可为空', trigger: 'blur'}],
                }
            }
        },
        methods: {
            login() {
                this.$refs.Manager.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        postRequest('/manager/login',
                            {
                                'username': this.Manager.username,
                                'password': this.Manager.password
                            }
                        ).then(res => {
                            // 登录密码校验成功
                            if (res && res.flag === '1') {
                                this.loading = false
                                // 保存登录信息
                                sessionStorage.setItem('isLogin', '1')
                                this.$store.commit('setLoginParam', res.user)
                                this.$message.success('❀ ❀ ❀     ' + res.user.username + '   登陆成功！');
                                // replace 不可回退  铺设可以
                                this.$router.push('/ManagerHome')
                            } else {
                                this.loading = false
                                // 登录失败
                                this.Manager.password = ''
                                this.$message.error(res.msg + '!');
                            }
                        })

                    } else {
                        this.$message.warning('请填写用户名或密码');
                        return false;
                    }
                })
            }
        },
    }
</script>

<style scoped>
  .loginContainer {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .loginTitle {
    margin: 0px auto 40px auto;
    text-align: center;
  }
</style>
